import { InjectionKey } from 'vue'
import { createStore } from 'vuex'
import {PageData,List} from './types'
import config from '../../project.config'

function addresJoin(url:string){
	var src=url.indexOf('http')!=-1?url:config.ServerAddres+url;
	return src;
}
export const store = createStore({
  state:{
		token:null,
		pageData:{},
		ServerAddred:'',
		list:[],
		ListLen:0,
	},
  mutations: {
		setPageData(state:any,data:PageData){
			data.HeadImg=addresJoin(data.HeadImg)
			state.pageData=data;
		},
		setList(state:any,list:Array<List>){
			list.forEach(item=>{
				item.img=addresJoin(item.img)
				state.list.push(item)
			});
		},
		setToken(state:any,token:string){
			localStorage.setItem('token',token)
			console.log('settoken',token)
			state.token=token;
		}
  },
	getters:{
		getToken(state:any){
			var token=state.token||localStorage.getItem('token');
			return token
		},
		getList(state:any){
			return state.list
		},
		getPageData(state:any){
			return state.pageData;
		},
		getListLen(state:any){
			return parseInt(state.list.length/10+'')
		}
	}
})
 
